/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package prettypenny;
import java.sql.*;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;

public class BookingList extends AbstractTableModel {
    private ArrayList<Booking> bookings;
    private Connection con;
    private Statement stmt;
    private ResultSet rs;
    private int position;
    private MechanicList mechanics;
    private String[] columnNames = {"Service ID", "Car ID", "Location", "Mechanic", "Time & Date", "Description", "Miles", "Price"};
    
         /*
     * Table
     */
    // * carService: serviceID, clientId, carId, locationId, mechanicId, typeId, serviceDate, serviceTime, description, price, bookingDate, mileage
    public int getColumnCount() {
        return columnNames.length;
    }

    public int getRowCount() {
        return bookings.size();
    }
    public String getColumnName(int col) {
        return columnNames[col];
    }
    public Object getValueAt(int rowIndex, int columnIndex) { 
       switch (columnIndex) {
                case 0:
                    return bookings.get(rowIndex).getServiceID();
                case 1:
                    return bookings.get(rowIndex).getCarID();
                case 2:
                    return bookings.get(rowIndex).getLocationID();
                case 3:
                    System.out.println(bookings.get(rowIndex));
                    System.out.println(bookings.get(rowIndex).getMechanicID());
                    return mechanics.getPosition(bookings.get(rowIndex).getMechanicID()).getName();
                case 4:
                    return bookings.get(rowIndex).getServiceDate() + bookings.get(rowIndex).getServiceTime();
                case 5:
                    return bookings.get(rowIndex).getServiceDesc();
                case 6:
                    return bookings.get(rowIndex).getMileage();
                case 7:
                    return bookings.get(rowIndex).getPrice();
            }
        return null;
    }
    
     /* DB Struture
     * car: carId, make, model, fuelType, regNum
     * carService: serviceID, clientId, carId, locationId, mechanicId, typeId, serviceDate, serviceTime, description, price, bookingDate, mileage
     * client: clientId, forname, surname, dob, addLine1, addLine2, city, county, postcode, homeNum, mobileNum, email
     * location: locationId, branch
     * mechanic: mechanicId, forename, surname, mobNum
     * serviceType: typeId, typeOfService
     */
    public BookingList() {
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // server:port:database
            con = DriverManager.getConnection("jdbc:odbc:uni");
            stmt = con.createStatement();
            RefreshData();
        }catch(Exception ex) {
            System.out.println(ex.toString());
        }
    }
     public BookingList(MechanicList mechanics) {
         this.mechanics = mechanics;
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // server:port:database
            con = DriverManager.getConnection("jdbc:odbc:uni");
            stmt = con.createStatement();
            RefreshData();
        }catch(Exception ex) {
            System.out.println(ex.toString());
        }
    }
   public void RefreshData() {
            // Clients
        try {
            bookings = new ArrayList<Booking>();
            rs = stmt.executeQuery("SELECT * FROM carService");
            //carService: serviceID, clientId, carId, locationId, mechanicId, typeId, serviceDate, serviceTime, description, price, bookingDate, mileage
            while (rs.next()) { //* carService: serviceID, clientId, carId, locationId, mechanicId, typeId, serviceDate, serviceTime, serviceDurationHour, description, price, bookingDate, mileage
                bookings.add(new Booking(rs.getInt(1), rs.getInt(2), rs.getInt(3), rs.getInt(4), rs.getInt(5), rs.getInt(6), rs.getString(7), rs.getString(8), rs.getInt(9), rs.getString(10), rs.getDouble(11), rs.getString(12), rs.getInt(13)));
            }
        }catch(Exception ex) {
            System.out.println("SS"+ex.toString());
        }
    }
    public ArrayList getClients() {
        return this.bookings;
    }

    public Booking getFirst() {
        position = 0;
        return bookings.get(position);
    }

    public Booking getLast() {
        position = bookings.size() - 1;
        return bookings.get(position);
    }

    public Booking getNext() {
        if (position < bookings.size()) {
            position++;
        }
        return bookings.get(position);
    }

    public Booking getPosition(int value) {
        return bookings.get(value);
    }

    public Booking getPrevious() {
        if (position > 0) {
            position--;
        }
        return bookings.get(position);
    }

    public int getSize() {
        return bookings.size() - 1;
    }
}
